/*
 * PegGameBoard.java
 *
 * Created on October 4, 2007, 10:54 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package peggame;

import java.util.Arrays;

/**
 *
 * @author Eric Drucker
 */
public class PegGameBoard
{
	
	private final int BOARD_SIZE = 5;
	
	private boolean[][] pegs;
	
	/** Creates a new instance of PegGameBoard */
	public PegGameBoard()
	{
		pegs = new boolean[BOARD_SIZE][];
	
		for(int i = 0; i < BOARD_SIZE; i++)
		{
			pegs[i] = new boolean[i+1];
			Arrays.fill(pegs[i], true);
		}
	}
	
	/**
	 * Gets the location of all pegs on the board
	 * @return The list of spaces and pegs
	 */
	public boolean[][] getPegs()
	{
		return pegs;
	}
	
	/**
	 * Removes one peg from the board - allows user to define which space is empty at beginning of game.
	 * @param pegCoords The coordinates of the peg to be removed
	 */
	public void removeFirstPeg(int[] pegCoords)
	{
		if(pegCoords.length != 2 || pegCoords[0] > pegs.length || pegCoords[0] < 0 || pegCoords[1] > pegs[pegCoords[0]].length || pegCoords[1] < 0)
		{
			System.err.println("Incorrect coordinates passed in removeFirstPeg");
			System.exit(0);
		}
		
		else
		{
			pegs[pegCoords[0]][pegCoords[1]] = false;
		}
	}
	
	
	/**
	 * Gets the number of possible moves in the board
	 * @return The number of possible moves
	 */
	public int numMovesAvailable()
	{
		//for each hole
		//if there is a peg
		//see if it can make a move (some will have more than one) NE, E, SE, SW, W, NW
		
		int numMoves = 0;
		
		for(int i = 0; i < BOARD_SIZE; i++)
		{
			for(int j = 0; j < pegs[i].length; j++)
			{
				if(pegs[i][j])	//if there is a peg
				{					
					//NORTHEAST
					if(i > 1)
					{
						if(j < pegs[i-2].length)
						{
							if(pegs[i-1][j] && !pegs[i-2][j])
							{
								numMoves++;
							}
						}
					}
					//EAST
					if(j < pegs[i].length-2)
					{
						if(pegs[i][j+1] && !pegs[i][j+2])
						{
							numMoves++;
						}
					}
					
					//SOUTHEAST
					if(j < pegs[i].length)
					{
						if(i < BOARD_SIZE-2)
						{
							if(pegs[i+1][j+1] && !pegs[i+2][j+2])
							{
								numMoves++;
							}
						}
					}
					
					//SOUTHWEST
					if(i < BOARD_SIZE-2)
					{
						if(pegs[i+1][j] && !pegs[i+2][j])
						{
							numMoves++;
						}
					}
					
					
					//WEST
					if(j > 1)
					{
						if(pegs[i][j-1] && !pegs[i][j-2])
						{
							numMoves++;
						}
					}
					
					//NORTHWEST
					if(j > 1)
					{
						if(i > 1)
						{
							if(pegs[i-1][j-1] && !pegs[i-2][j-2])
							{
								numMoves++;
							}
						}
					}
				}
			}
		}
		
		return numMoves;
	}
	
	/**
	 * Processes a move on the board.
	 * @param start The coordinates of the peg to be moved
	 * @param finish The final coordinates of the moving peg
	 * @return True if move is valid, false otherwise
	 */
	public boolean move(int[] start, int[] finish)
	{
		if(start.length != 2 || finish.length != 2)
		{
			System.err.println("Incorrect coordinates passed in move: " + Arrays.toString(start) + " " + Arrays.toString(finish));
			return false;
		}
		
		if(start[0] > pegs.length || start[0] < 0 || start[1] > pegs[start[0]].length || start[1] < 0 || finish[0] > pegs.length || finish[0] < 0 || finish[1] > pegs[finish[0]].length || finish[1] < 0)
		{
			System.err.println("Incorrect coordinates passed in move: " + Arrays.toString(start) + " " + Arrays.toString(finish));
			return false;
		}
		
		//NE
		if(start[0]-finish[0] == 2 && start[1]-finish[1] == 0)
		{
			if(pegs[start[0]][start[1]] && pegs[start[0]-1][start[1]] && !pegs[finish[0]][finish[1]])
			{
				pegs[start[0]][start[1]] = false;
				pegs[start[0]-1][start[1]] = false;
				pegs[finish[0]][finish[1]] = true;
				
				return true;
			}
		}
		
		//E
		if(start[0] == finish[0] && start[1]-finish[1] == -2)
		{
			if(pegs[start[0]][start[1]] && pegs[start[0]][start[1]+1] && !pegs[finish[0]][finish[1]])
			{
				pegs[start[0]][start[1]] = false;
				pegs[start[0]][start[1]+1] = false;
				pegs[finish[0]][finish[1]] = true;
				
				return true;
			}
		}
		
		//SE
		if(start[0]-finish[0] == -2 && start[1]-finish[1] == -2)
		{
			if(pegs[start[0]][start[1]] && pegs[start[0]+1][start[1]+1] && !pegs[finish[0]][finish[1]])
			{
				pegs[start[0]][start[1]] = false;
				pegs[start[0]+1][start[1]+1] = false;
				pegs[finish[0]][finish[1]] = true;
				
				return true;
			}
		}
		
		//SW
		if(start[0]-finish[0] == -2 && start[1]-finish[1] == 0)
		{
			if(pegs[start[0]][start[1]] && pegs[start[0]+1][start[1]] && !pegs[finish[0]][finish[1]])
			{
				pegs[start[0]][start[1]] = false;
				pegs[start[0]+1][start[1]] = false;
				pegs[finish[0]][finish[1]] = true;
				
				return true;
			}
		}
		
		//W
		//E
		if(start[0] == finish[0] && start[1]-finish[1] == 2)
		{
			if(pegs[start[0]][start[1]] && pegs[start[0]][start[1]-1] && !pegs[finish[0]][finish[1]])
			{
				pegs[start[0]][start[1]] = false;
				pegs[start[0]][start[1]-1] = false;
				pegs[finish[0]][finish[1]] = true;
				
				return true;
			}
		}
		
		//NW
		if(start[0]-finish[0] == 2 && start[1]-finish[1] == 2)
		{
			if(pegs[start[0]][start[1]] && pegs[start[0]-1][start[1]-1] && !pegs[finish[0]][finish[1]])
			{
				pegs[start[0]][start[1]] = false;
				pegs[start[0]-1][start[1]-1] = false;
				pegs[finish[0]][finish[1]] = true;
				
				return true;
			}
		}
		
		System.err.println("Invalid Move");
		return false;
	}


	/**
	 * Gets the size of the board
	 * @return size of the board
	 */
	public int getBoardSize()
	{
		return BOARD_SIZE;
	}
	
	/**
	 * Creates a text based representation of the game board
	 * @return text based representation of the board
	 */
	public String toString()
	{
		String ret = "";
		
		for(int i = 0; i < BOARD_SIZE; i++)
		{
			for(int k = i; k < BOARD_SIZE-1; k++)
			{
				ret += " ";
			}
			
			for(int j = 0; j < pegs[i].length; j++)
			{
				if(pegs[i][j])
				{
					ret += "1 ";
				}
				else
				{
					ret += "0 ";
				}
			}
			
			ret += "\n";
		}
		
		return ret;
	}
}
